package com.bkgy.modular.subjectQuery.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.calsidata.entity.Evaluate;
import com.bkgy.modular.calsidata.entity.TbEdge;
import com.bkgy.modular.calsidata.entity.TbFalt;
import com.bkgy.modular.datacollect_bx.bean.JDataSave621;
import com.bkgy.modular.datacollect_bx.bean.JDataSave622;
import com.bkgy.modular.datacollect_bx.bean.JDataSave642;
import com.bkgy.modular.subjectQuery.entity.HotRollingPDI;
import com.bkgy.modular.subjectQuery.service.CurveService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName CurveServiceImpl
 * @date 2023/1/8 15:49
 */
@Service
public class CurveServiceImpl implements CurveService {
    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public ResultData findCurveData(JSONObject jsonObject) {
        //获取前端传入参数
        String volumeNo = jsonObject.getString("volumeNo");
        String curveKey = jsonObject.getString("curveKey");
        // 根据volumeNo查询出数据
        Query query = new Query(Criteria.where("volumeNo").is(volumeNo));
        List<HotRollingPDI> list = mongoTemplate.find(query, HotRollingPDI.class);

        // 遍历list集合获取每条数据
        for (HotRollingPDI item : list) {
            if (item.getOriginBolg() == null) {
                return ResultData.ResultDataFaultSelf("数据为空", null);
            }
            // 在获取每条数据中的bolg数据
            Map<String, List<Double>> originBolg = item.getOriginBolg();
            // 根据传入key进行查询曲线数据
            List<Double> doubleList = originBolg.get(curveKey);
            return ResultData.ResultDataFaultSelf("查询曲线数据成功", doubleList);
        }
        return ResultData.ResultDataFaultSelf("查询曲线数据失败", null);
    }

    @Override
    public ResultData findColdRollingData(JSONObject jsonObject) {
        //获取前端传入参数
        String volumeNo = jsonObject.getString("volumeNo");
        String curveKey = jsonObject.getString("curveKey");
        // 根据volumeNo查询出数据
        Query query = new Query(Criteria.where("extid").is(volumeNo));
        List<Evaluate> evaluateList = mongoTemplate.find(query, Evaluate.class);
        List<TbEdge> tbEdgeList = mongoTemplate.find(query, TbEdge.class);
        List<JDataSave621> jDataSave621List = mongoTemplate.find(query, JDataSave621.class);
        List<TbFalt> tbFaltList = mongoTemplate.find(query, TbFalt.class);
        List<JDataSave622> jDataSave622List = mongoTemplate.find(query, JDataSave622.class);
        List<JDataSave642> jDataSave642List = mongoTemplate.find(query, JDataSave642.class);

        // 1720Evaluate曲线数据
        if (evaluateList.size() > 0) {
            for (Evaluate evaluate : evaluateList) {
                Map<String, List<Double>> edge = evaluate.getEdge();
                List<Double> list = edge.get(curveKey);
                if (list != null && !list.isEmpty()) {
                    return ResultData.ResultDataFaultSelf("查询成功", list);
                }
            }
        }
        // 1720TbEdge曲线数据
        if (!ObjectUtils.isEmpty(jDataSave621List) && !ObjectUtils.isEmpty(jDataSave621List.get(0)) && !ObjectUtils.isEmpty(jDataSave621List.get(0).getRow().get(curveKey))) {
            // List<Object> list = new ArrayList<>();
            for (JDataSave621 jDataSave621 : jDataSave621List) {
                Map<String, List<Double>> row = jDataSave621.getRow();
                Object obj = row.get(curveKey);
                if (obj != null) {
                    // list.add(obj);
                    return ResultData.ResultDataFaultSelf("查询成功", obj);
                }
            }
        } else if (tbEdgeList.size() > 0) {
            // List<Object> list = new ArrayList<>();
            for (TbEdge tbEdge : tbEdgeList) {
                Map<String, Object> edge = tbEdge.getEdge();
                Object obj = edge.get(curveKey);
                if (obj != null) {
                    // list.add(obj);
                    return ResultData.ResultDataFaultSelf("查询成功", obj);
                }
            }
        }
        // 1720JDataSave621曲线数据
        if (jDataSave621List.size() > 0) {
            for (JDataSave621 jDataSave621 : jDataSave621List) {
                Map<String, List<Double>> row = jDataSave621.getRow();
                List<Double> list = row.get(curveKey);
                if (list != null && !list.isEmpty()) {
                    return ResultData.ResultDataFaultSelf("查询成功", list);
                }
            }
        }
        // 2130TbFalt曲线数据
        if (!ObjectUtils.isEmpty(jDataSave622List) && !ObjectUtils.isEmpty(jDataSave622List.get(0)) && !ObjectUtils.isEmpty(jDataSave622List.get(0).getRow().get(curveKey))) {
            // List<Object> list = new ArrayList<>();
            for (JDataSave622 jDataSave622 : jDataSave622List) {
                Map<String, List<Double>> row = jDataSave622.getRow();
                Object obj = row.get(curveKey);
                if (obj != null) {
                    // list.add(obj);
                    return ResultData.ResultDataFaultSelf("查询成功", obj);
                }
            }
        } else if (tbFaltList.size() > 0) {
            // List<Object> list = new ArrayList<>();
            for (TbFalt tbFalt : tbFaltList) {
                Map<String, Object> edge = tbFalt.getEdge();
                Object obj = edge.get(curveKey);
                if (obj != null) {
                    // list.add(obj);
                    return ResultData.ResultDataFaultSelf("查询成功", obj);
                }
            }
        }
        // 2130jDataSave622曲线数据
        if (jDataSave622List.size() > 0) {
            for (JDataSave622 jDataSave622 : jDataSave622List) {
                Map<String, List<Double>> row = jDataSave622.getRow();
                List<Double> list = row.get(curveKey);
                if (list != null && !list.isEmpty()) {
                    return ResultData.ResultDataFaultSelf("查询成功", list);
                }
            }
        }

        // 2130连退jDataSave642曲线数据
        if (jDataSave642List.size() > 0) {
            for (JDataSave642 jDataSave642 : jDataSave642List) {
                Map<String, List<Double>> row = jDataSave642.getRow();
                List<Double> list = row.get(curveKey);
                if (list != null && !list.isEmpty()) {
                    return ResultData.ResultDataFaultSelf("查询成功", list);
                }
            }
        }
        return ResultData.ResultDataFaultSelf("查询失败,数据为空", null);
    }
}
